import wxValidate from 'wechat-validate'
Page({
  behaviors: [wxValidate],
  /**
   * 页面的初始数据
   */
  data: {
    layerObj: {
      type: {
        dataList: [
          {
            name: '维修',
            val: 'repair'
          },
          {
            name: '房间',
            val: 'room'
          },
          {
            name: '访客',
            val: 'visitor'
          }
        ],
        title: '类型'
      },
      data: {
        dataList: [],
        title: '数据'
      },
      status: {
        dataList: {
          repair: [
            {
              name: '受理中',
              val: '1'
            },
            {
              name: '上门中',
              val: '2'
            },
            {
              name: '已完成',
              val: '3'
            },
            {
              name: '已取消',
              val: '0'
            }
          ],
          room: [
            {
              name: '审核中',
              val: '1'
            },
            {
              name: '审核成功',
              val: '2'
            },
            {
              name: '审核失败',
              val: '3'
            }
          ],
          visitor: [
            {
              name: '生效中',
              val: '1'
            },
            {
              name: '已失效',
              val: '0'
            }
          ]
        },
        title: '状态'
      }
    },
    current: 'type',
    layerVisible: false,
    typeItemName: '维修',
    typeItemVal: 'repair',
    dataItemName: '',
    dataItemVal: '',
    statusItemName: '',
    statusItemVal: ''
  },

  rules: {
    dataItemName: [
      { required: true, message: '请选择数据！' }
    ],
    statusItemName: [
      { required: true, message: '请选择状态！' }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.selectInfo({
      detail: {
        name: '维修',
        val: 'repair'
      }
    })
  },

  openLayer (ev) {
    this.setData({
      current: ev.mark.val,
      layerVisible: true
    })
  },

  closeLayer () {
    this.setData({
      layerVisible: false
    })
  },

  selectInfo (ev) {
    switch (this.data.current) {
      case 'type':
        this.setData({
          typeItemName: ev.detail.name,
          typeItemVal: ev.detail.val,
          dataItemName: '',
          dataItemVal: '',
          statusItemName: '',
          statusItemVal: ''
        })
        switch (ev.detail.val) {
          case 'repair':
            (async () => {
              const {code, data} = await wx.http.get('/repair')
              if (code !== 10000) return wx.utils.toast()
              const layerObj = this.data.layerObj
              layerObj.data.dataList = data.rows.map(item => {
                return {
                  name: item.houseInfo,
                  val: item.id
                }
              })
              this.setData({
                layerObj
              })
            })()
            break
          case 'room':
            (async () => {
              const {code, data} = await wx.http.get('/room')
              if (code !== 10000) return wx.utils.toast()
              const layerObj = this.data.layerObj
              layerObj.data.dataList = data.map(item => {
                return {
                  name: `${item.point}${item.building}${item.room}`,
                  val: item.id
                }
              })
              this.setData({
                layerObj
              })
            })()
            break
          case 'visitor':
            (async () => {
              const {code, data} = await wx.http.get('/visitor')
              if (code !== 10000) return wx.utils.toast()
              const layerObj = this.data.layerObj
              layerObj.data.dataList = data.rows.map(item => {
                return {
                  name: item.houseInfo,
                  val: item.id
                }
              })
              this.setData({
                layerObj
              })
            })()
            break
        }
        break
      case 'data':
        this.setData({
          dataItemName: ev.detail.name,
          dataItemVal: ev.detail.val
        })
        break
      case 'status':
        this.setData({
          statusItemName: ev.detail.name,
          statusItemVal: ev.detail.val
        })
        break
    }
    this.setData({
      layerVisible: false
    })
  },

  async submitForm () {
    if (!this.validate()) return
    const {code} = await wx.http.get('/help/updateStatus', {
      type: this.data.typeItemVal,
      id: this.data.dataItemVal,
      status: this.data.statusItemVal
    })
    if (code !== 10000) return wx.utils.toast()
    wx.utils.toast('修改成功')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})